# -*- coding: utf-8 -*-


import threading
from collections import deque

from contract_market.error.error import QueueIndexError

no_value = None

class PriorityQueue:
    """ """

    def __init__(self, maxlen=100) -> None:
        """ """

        self._lock = threading.Lock()
        self._deque = deque(maxlen=maxlen)
        self._full_len = maxlen
        self._cond = threading.Condition(self._lock)

    def put(self, item):
        """ """
        with self._cond:
            self._deque.append(item)
            self._cond.notify_all()

    def get(self):
        """ """
        with self._cond:
            self._cond.wait()
            return self._get_data()

    def empty(self):
        """ """
        with self._cond:
            return len(self._deque) == 0

    def full(self):
        """ """
        with self._cond:
            return len(self._deque) == self._full_len

    def _get_data(self):
        try:
            return self._deque.popleft()
        except IndexError as e:
            raise QueueIndexError("deque index out of range") from e

    def get_notwait(self):
        with self._cond:
            if len(self._deque) <=0:
                return no_value
            else:
                return self._get_data()